package st.business.tepth.business.user.busnumber.workholder.maintain.pass;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.MaintainPassInfoBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * @author zhangyu
 * @date 2019/4/19
 */
public class MaintainPassModel implements MaintainPassContract.Model {
    @Override
    public Disposable getData(String token, int repairId, final OnDataConvertListener<List<MaintainPassInfoBean>> listener) {
        return RxRestClient.builder()
                .url(Url.MaintainPassInfo)
                .params("repairId", repairId)
                .params("token", token)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertMaintainPassInfo(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        //listener.convertMaintain(convertDebugInfo());
                        listener.error(code, msg);
                    }
                })
                .build()
                .get();
    }

    @Override
    public Disposable checkNotPass(String token, String repairTaskList, String comment, final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.MaintainCheckNotPass)
                .params("repairTaskList", repairTaskList)
                .params("token", token)
                .params("comment", comment)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        //listener.convertMaintain(convertDebugInfo());
                        listener.error(code, msg);
                    }
                })
                .build()
                .get();
    }

    @Override
    public Disposable checkPass(String token, String repairTaskList, final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.MaintainCheckPass)
                .params("repairTaskList", repairTaskList)
                .params("token", token)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        //listener.convertMaintain(convertDebugInfo());
                        listener.error(code, msg);
                    }
                })
                .build()
                .get();
    }

    @Override
    public Disposable cancelFinishWork(String token, int repairId,
                                       final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.CancelFinishWork)
                .params("repairId", repairId)
                .params("token", token)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .get();
    }

    private List<MaintainPassInfoBean> convertMaintainPassInfo(String response) {
        return new ObjConverter<List<MaintainPassInfoBean>>().convert(response
                , new TypeReference<List<MaintainPassInfoBean>>() {
                });
    }

}
